﻿package org.openChart.meta.manager 
{
	import org.openChart.meta.manager.CSSLoader;
	import org.openChart.meta.PieChart;
	import org.openChart.meta.Style;
	import org.openChart.meta.data.GroupSet;
	import org.openChart.meta.TextLabel;
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class PieChartBuilder extends XmlBase
	{
		
		public function PieChartBuilder() 
		{
			
		}
		
		public function buildPieChart( xml:XML, chartType:String ):PieChart
		{
			var object:Object = CSSLoader.instance().getStyle(CSSLoader.default_pie_style);
			var style:Style = new Style(object);
			//trace(this + " pie color:" + style.color);
			//trace(this + " alpha from xml is " + xml.@alpha);
			style = this.buildStyle(xml, style);
			
			var pie:PieChart = new PieChart(style);
			//trace(this + " pie color:" + pie.getStyle().alpha);
			object = CSSLoader.instance().getStyle(CSSLoader.chart_title);
			style = new Style(object, style);
			
			var title:TextLabel = null;
			if (new String(xml.title) != "")
				title = buildLabel(xml.title[0], style);
			pie.title = title;
			
			var xmlDataLoader:XmlDataLoader = new XmlDataLoader();
			var labels:Array;
			
			if (new String(xml.labels) != "" )
				labels = xmlDataLoader.buildUpLabels(xml);
			//trace(this + " pie label " + labels.length);
			if (labels)
				pie.labels = labels;
			if (new String(xml.datas[0]) != "")
			{
				
				labels= xmlDataLoader.buildUpLabels(xml.datas[0]);
				if (labels)
					pie.labels = labels;
				var groupSet:GroupSet = xmlDataLoader.buildGroupSet(xml.datas[0], chartType);
				if (groupSet)
					pie.groupSet = groupSet;
			}
			
			for ( var i:uint = 0; i < pie.labels.length; i++ )
			{
				var label:TextLabel = pie.labels[i] as TextLabel;
			}
			return pie;
		}
	}
	
}